package com.uchedao.seller.db;

import android.content.Context;
import android.text.TextUtils;

import com.uchedao.seller.SellerApplication;
import com.uchedao.seller.util.PreferenceUitl;


public class User {
	// 2505600000
	private static final long AUTO_LOIGN_ACTIVE_TIME = 1000L * 60 * 60 * 24 * 29;// 29天
	private static final String KEY_NAME = "name";// 用户名
	private static final String KEY_PWD = "pwd";// 密码
	private static final String KEY_TOKEN = "token";// token
	private static final String KEY_REMEMBER_PWD = "rememberPwd";// 记住密码
	private static final String KEY_AUTO_LOGIN = "autoLogin";// 自动登录
	private static final String KEY_LOGIN_TIME = "loginTime";// 登录时间
	private static User user;
	private PreferenceUitl pu = new PreferenceUitl(SellerApplication.getContext(), "user", Context.MODE_PRIVATE);

	private String name;// 用户名

	private String pwd;// 密码

	private String token;// 令牌

	private boolean rememberPwd = true;// 记住密码

	private boolean autoLogin;// 自动登录

	private User() {
		if (!pu.contains(KEY_NAME)) {
			pu.clear();
			return;
		}
		reset();
	}

	/**
	 * 重置数据
	 */
	public void reset() {
		name = pu.getString(KEY_NAME);
		pwd = pu.getString(KEY_PWD);
		token = pu.getString(KEY_TOKEN);
		rememberPwd = pu.getBoolean(KEY_REMEMBER_PWD);
		autoLogin = pu.getBoolean(KEY_AUTO_LOGIN);
	}

	public static User getInstance() {
		if (user == null)
			user = new User();
		return user;
	}

	/**
	 * 设置用户信息
	 * 
	 * @param name
	 * @param pwd
	 * @param token
	 * @param rememberPwd
	 * @param autoLogin
	 */
	public void setUserInfo(String name, String pwd, String token, boolean rememberPwd, boolean autoLogin) {
		this.name = name;// 用户名
		this.pwd = pwd;// 密码
		this.token = token;// 令牌
		this.rememberPwd = rememberPwd;// 记住密码
		this.autoLogin = autoLogin;// 自动登录
		pu.putString(KEY_NAME, name);
		pu.putString(KEY_PWD, pwd);
		if (autoLogin)
			pu.putString(KEY_TOKEN, token);
		if (rememberPwd)
			pu.putBoolean(KEY_REMEMBER_PWD, rememberPwd);
		pu.putBoolean(KEY_AUTO_LOGIN, autoLogin);
		pu.putLong(KEY_LOGIN_TIME, System.currentTimeMillis());
	}

	public void logout() {
		this.name = null;// 用户名
		this.pwd = null;// 密码
		token = null;// 令牌
		rememberPwd = true;// 记住密码
		autoLogin = false;// 自动登录
		//
		// pu.remove(KEY_TOKEN);
		// autoLogin = false;// 自动登录
		// pu.putBoolean(KEY_AUTO_LOGIN, false);

		pu.clear();
	}

	public PreferenceUitl getPu() {
		return pu;
	}

	public void setPu(PreferenceUitl pu) {
		this.pu = pu;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public boolean isRememberPwd() {
		return rememberPwd;
	}

	public void setRememberPwd(boolean rememberPwd) {
		this.rememberPwd = rememberPwd;
	}

	public boolean isAutoLogin() {
		return autoLogin;
	}

	public void setAutoLogin(boolean autoLogin) {
		this.autoLogin = autoLogin;
	}

	/**
	 * 是否已登录
	 * 
	 * @return
	 */
	public boolean isLogin() {
		return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(token) && isTokenValid();
	}

	/**
	 * token是否有效
	 * 
	 * @return
	 */
	private boolean isTokenValid() {
		long loginTime = pu.getLong(KEY_LOGIN_TIME);
		long time = System.currentTimeMillis();
		return (time - loginTime) <= AUTO_LOIGN_ACTIVE_TIME;
	}
}
